package topofstack.networkserverutils.fragments;

import topofstack.networkserverutils.R;
import topofstack.networkserverutils.SmtpTest;
import topofstack.networkserverutils.bean.Location;
import topofstack.networkserverutils.bean.PingInfo;
import topofstack.networkserverutils.bean.TraceRouteInfo;
import topofstack.networkserverutils.net.GeoLocation;
import topofstack.networkserverutils.net.Nslookup;
import topofstack.networkserverutils.net.Ping;
import topofstack.networkserverutils.net.TraceRoute;
import topofstack.networkserverutils.net.Whois;
import topofstack.networkserverutils.util.Constants;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ServerTestingFunctions extends Fragment {
	
	private Spinner functionList;
	private EditText urlField;
	private Button submit;
	private TextView heading;
	private TextView result;
	
	private ProgressDialog dialog;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View convertView = inflater.inflate(R.layout.functions, container, false);
		
		functionList = (Spinner) convertView.findViewById(R.id.functions_list);
		urlField = (EditText) convertView.findViewById(R.id.functions_url);
		submit = (Button) convertView.findViewById(R.id.functions_submit);
		heading = (TextView) convertView.findViewById(R.id.functions_heading);
		result = (TextView) convertView.findViewById(R.id.functions_result);
		
		String[] functionNames = getResources().getStringArray(R.array.function_names);
		ArrayAdapter<String> functionNameAdapter
			= new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, functionNames);
		functionNameAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		functionList.setAdapter(functionNameAdapter);
		
		submit.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				if(urlField.getText().toString().trim().length() == 0)
					Toast.makeText(getActivity(), Constants.ERROR_EMPTY_FIELD,
							Toast.LENGTH_SHORT).show();
				else {
					if(functionList.getSelectedItemPosition() == 5) {
						Intent intent = new Intent(getActivity(), SmtpTest.class);
						intent.putExtra(SmtpTest.HOST_NAME, urlField.getText().toString());
						startActivity(intent);
					}
					else {
						new RetriveData().execute(new Void[0]);
						
						dialog = new ProgressDialog(getActivity());
						dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
						dialog.setMessage(Constants.DIALOG_MESSAGE);
						dialog.setIndeterminate(true);
						dialog.show();
					}
				}
			}
		});
		
		return convertView;
	}
	
	private class RetriveData extends AsyncTask<Void, Void, String> {
		
		private String url;
		
		@Override
		protected String doInBackground(Void... params) {
			
			url = urlField.getText().toString();
			
			int index = functionList.getSelectedItemPosition();
			switch(index) {
				case 0: PingInfo pingInfo = Ping.get(url);
					if(pingInfo == null)
						return "Failed";
					else return pingInfo.toString();
				case 1: TraceRouteInfo traceRouteInfo = TraceRoute.get(url);
					if(traceRouteInfo == null)
						return null;
					else
						return traceRouteInfo.toString();
				case 2: return Nslookup.get(url);
				case 3: return Whois.get(url, false);
				case 4: Location location = GeoLocation.get(url);
					if(location == null)
						return url + " has no LOC record";
					else
						return location.toString();
			}
			
			return null;
		}
		
		@Override
		protected void onPostExecute(String resultString) {
			if(resultString == null)
				Toast.makeText(getActivity(), Constants.ERROR_MESSAGE, Toast.LENGTH_SHORT)
					.show();
			else {
				heading.setText(functionList.getSelectedItem().toString() + " : " + url);
				result.setText(resultString);
				heading.setBackgroundResource(android.R.color.darker_gray);
			}
			
			dialog.cancel();
		}
	}
}
